/**
 * Created by charsen on 16/6/1.
 */
directives.directive('delSource', ["$rootScope", "tools", "$timeout", function ($rootScope, tools, $timeout) {
    return {
        restrict: 'AE',
        scope: {},
        replace: true,
        link: function (scope, element, attr) {
            scope.sourceData = [];

            scope.delSour = false;
            scope.delSourTable = false;

            scope.msg = null;
            scope.sourceIndex = null;
            $rootScope.$on('delSource_EV', function (e,data,key) {
                if(key==$rootScope.key){
                    $(element).modal('show');

                    scope.sourceData = data;

                    scope.pagination = {
                        current: 1
                    };

                    console.log(data);
                    console.log("show");
                    $rootScope.key = Math.floor(Math.random()*1000000000);
                }
            });

            //show panel
            scope.showDelSource = function(index){
                scope.sourceIndex = index;
                scope.delSour = true;
            };
            //删除数据源

            scope.confimDelSource = function(){
                var sourceId = scope.sourceData[scope.sourceIndex].sourceId;
                tools.get({
                    url:"/Ibm-Client/deleteDataSource.htm",
                    data:{
                        "sourceId":sourceId
                    },
                    succ:function(resp){

                        if(resp.success){
                            $rootScope.$broadcast("addSource_Update_EV");
                            scope.sourceData.splice(scope.sourceIndex,1);
                            scope.delSour = false;
                            scope.sourceIndex=null;
                        }else{
                            scope.msg = "删除数据源，请先清空该数据源的全部表!";
                            $timeout(function(){
                                scope.msg=null;
                            },2000);
                        }
                    }
                });
            };



            scope.dbTables = {
                base: [],
                relation: [],
                filetable: [],
                aggregation: [],
                mulRelation: []
            };
            // 获取数据源基础数据--base
            function getIbmTables(sourceId) {
                tools.get({
                    url: '/Ibm-Client/getIbmTables.htm',
                    method: 'POST',
                    data: {
                        type: 'table',
                        dataSourceId: sourceId
                    },
                    succ: function (resp) {
                        if (resp.success) {

                            if (!$.isEmptyObject(resp.value)) {
                                $timeout(function(){
                                    scope.dbTables.base = resp.value;
                                },0);

                            }
                        } else {
                            $timeout(function(){
                                $rootScope.$broadcast("MESSAGE_EV","数据异常，请找管理员联系。");
                                return;
                            },500);
                        }
                    }
                });
            }

            // 获取关联关系数据--relation
            /* function getIbmRelationTables(sourceId) {
             tools.get({
             url: '/Ibm-Client/getIbmRelationTables.htm',
             method: 'POST',
             data: {
             type: 'table',
             dataSourceId: sourceId
             },
             succ: function (resp) {
             if (resp.success) {

             if ($.isEmptyObject(resp.value)) {
             scope.dbTables.relation = resp.value;

             }
             } else {
             //alert(resp.message);
             $timeout(function(){
             $rootScope.$broadcast("MESSAGE_EV","数据异常，请找管理员联系。");
             return;
             },500);
             }
             }
             });
             }*/

            // 获取多表关联数据--mulRelation
            function getIbmMultiRelationTables(sourceId) {
                tools.get({
                    url: '/Ibm-Client/getMultiRelationTables.htm',
                    method: 'POST',
                    data: {
                        type: 'table',
                        dataSourceId: sourceId
                    },
                    succ: function (resp) {
                        if (resp.success) {

                            if (!$.isEmptyObject(resp.value)) {
                                $timeout(function(){
                                    scope.dbTables.mulRelation = resp.value;
                                },0);

                            }
                        } else {
//                    alert(resp.message);
                            $timeout(function(){
                                $rootScope.$broadcast("MESSAGE_EV","数据异常，请找管理员联系。");
                                return;
                            },500);
                        }
                    }
                });
            }

            // 获取分类汇总数据--aggregation
            function getIbmAggregateFunctions(sourceId) {
                tools.get({
                    url: '/Ibm-Client/getIbmAggregateFunctions.htm',
                    method: 'POST',
                    data: {
                        type: 'table',
                        dataSourceId: sourceId
                    },
                    succ: function (resp) {
                        if (resp.success) {

                            if (!$.isEmptyObject(resp.value)) {
                                $timeout(function(){
                                    scope.dbTables.aggregation = resp.value;
                                },0);
                            }
                        } else {
//                    alert(resp.message);
                            $timeout(function(){
                                $rootScope.$broadcast("MESSAGE_EV","数据异常，请找管理员联系。");
                                return;
                            },500);
                        }
                    }
                });
            }

            // 获取上传数据--fileTable
            function getFileTableFunctions(sourceId) {
                tools.get({
                    url: '/Ibm-Client/getFileTables.htm',
                    method: 'POST',
                    data: {
                        type: 'table',
                        dataSourceId: sourceId
                    },
                    succ: function (resp) {
                        if (resp.success) {

                            if (!$.isEmptyObject(resp.value)) {
                                $timeout(function(){
                                    scope.dbTables.filetable = resp.value;
                                },0);

                            }
                        } else {
//                    alert(resp.message);
                            $timeout(function(){
                                $rootScope.$broadcast("MESSAGE_EV","数据异常，请找管理员联系。");
                                return;
                            },500);
                        }
                    }
                });
            }

            scope.showDelSourceTable = function(index){
                scope.sourceIndex = index;
                scope.delSourTable = true;
                var sourceId = scope.sourceData[scope.sourceIndex].sourceId;
                tools.get({
                    method: 'POST',
                    url: '/Ibm-Client/changeDataSource.htm',
                    data: {
                        sourceId: sourceId
                    },
                    succ: function (resp) {
                        if (resp.success) {
                            getIbmTables(sourceId);
//                            getIbmRelationTables(sourceId);
                            getIbmMultiRelationTables(sourceId);
                            getIbmAggregateFunctions(sourceId);
                            getFileTableFunctions(sourceId);

                        }
                    }
                });

            };


            //清空表
            function delSourceTables(data,type){
                if(!$.isEmptyObject(data)) {
                    for (var i=data.length-1;i>=0;i--) {
                        tools.get({
                            url:"/Ibm-Client/deleteTable.htm",
                            data:{
                                "tableType": data[i].tableType,
                                "tbId": data[i].tbId
                            },
                            succ:function(resp){
                                if(resp.success){
                                    data.splice(i,1);
                                }else{
                                    scope.msg = "清空表失败!";
                                    $timeout(function(){
                                        scope.msg=null;
                                    },2000);
                                    return;
                                }
                            }
                        });
                    }
                    $rootScope.$broadcast("UpdateSliderTable_EV",type);
                }
            }


            //清空表
            scope.confimDelSourceTable = function(type){

                switch(type){
                    case "base":
                        delSourceTables(scope.dbTables.base,"Ibm");

                        break;
                    case "mulRel":
                        delSourceTables(scope.dbTables.mulRelation,"mulRel");

                        break;
                    case "agg":
                        delSourceTables(scope.dbTables.aggregation,"Agg");

                        break;
                    case "file":
                        delSourceTables(scope.dbTables.filetable,"File");

                        break;
                    default :
                        break;
                }

                $timeout(function(){
                    $rootScope.$broadcast("dbManage_Init_EV");
                },500);


            };

            //关闭
            scope.closeDelSource = function(){
                $(element).modal("hide");
                $rootScope.$broadcast("dbManage_Init_EV");
                //$rootScope.$broadcast("UpdateSliderTable_EV","all");
            }

        },
        templateUrl: "/trend-new-BI/app/components/dbManager/delSource/delSourceView.html"
    }
}]);